package com.qy139.guest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qy139.entity.CustomerInfo;
import com.qy139.guest.mapper.CustomerInfoMapper;
import com.qy139.guest.service.ICustomerInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qy139.guest.util.RandomUtil;
import com.qy139.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yuhaochen
 * @since 2021-12-01
 */
@Service
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements ICustomerInfoService {
    @Resource
    private CustomerInfoMapper customerInfoMapper;

    @Override
    public Result addGuest(CustomerInfo customerInfo) {
        String cId = "";
        Calendar cal = new GregorianCalendar();
        while (true){
            int i = (int) (1000+(Math.random()*(9999-1000+1)));
            cId = RandomUtil.toDateString(cal)+i;
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("c_id",cId);
            CustomerInfo one = this.getOne(queryWrapper);
            if (one == null){
                break;
            }
        }
        customerInfo.setCId(cId);
        return new Result(customerInfoMapper.insert(customerInfo));
    }

    @Override
    public Result updOrUpdStatus(CustomerInfo customerInfo) {
        customerInfo.setUpdatetime(new Date());
        return new Result(customerInfoMapper.updateById(customerInfo));
    }
}
